package com.hatuan.vnexpress;

import java.util.ArrayList;
import java.util.List;

import com.hatuan.adapter.NavDrawerListAdapter;
import com.hatuan.models.CheckInternet;
import com.hatuan.models.MenuDrawer;
import com.hatuan.models.RssItem;
import com.hatuan.models.RssLink;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class VnexpressActivity extends Activity {
	// kiem tra internet
	private ActionBarDrawerToggle mDrawerToggle;
	private DrawerLayout mDrawerLayout;
	// list Drawer Menu sliding
	private ListView mDrawerList;
	// slide menu items
	private String[] navMenuTitles;
	private TypedArray navMenuIcons;
	// danh sach menuItem
	private ArrayList<MenuDrawer> navDrawerItems;
	// adapter menu sliding
	private NavDrawerListAdapter adapterMenuSliding;

	// nav drawer title
	private CharSequence mDrawerTitle;

	// used to store app title
	private CharSequence mTitle;

	@SuppressLint("ResourceAsColor")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActionBar ab = getActionBar();
		ColorDrawable colorDrawable = new ColorDrawable(
				Color.parseColor("#9f224e"));
		ab.setBackgroundDrawable(colorDrawable);
		setContentView(R.layout.activity_vnexpress);

		mTitle = mDrawerTitle = getTitle();

		navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
		navMenuIcons = getResources()
				.obtainTypedArray(R.array.nav_drawer_icons);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
		navDrawerItems = new ArrayList<MenuDrawer>();
		for (int i = 0; i < navMenuTitles.length; i++) {
			navDrawerItems.add(new MenuDrawer(navMenuTitles[i], navMenuIcons
					.getResourceId(i, -1)));

		}
		// Recycle the typed array
		navMenuIcons.recycle();
		mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
		adapterMenuSliding = new NavDrawerListAdapter(getApplicationContext(),
				navDrawerItems);
		mDrawerList.setAdapter(adapterMenuSliding);
		// enabling action bar app icon and behaving it as toggle button
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, // nav menu toggle icon
				R.string.app_name, // nav drawer open - description for
									// accessibility
				R.string.app_name // nav drawer close - description for
									// accessibility
		) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(mTitle);
				// calling onPrepareOptionsMenu() to show action bar icons
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(mDrawerTitle);
				// calling onPrepareOptionsMenu() to hide action bar icons
				invalidateOptionsMenu();
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);
		if (savedInstanceState == null) {
			// on first time display view for first nav item
			// displayView(0);

			if (CheckInternet.checkInternetConnection(getApplicationContext())) {
				Fragment fragment = new Srceen_Fragment();
				FragmentManager fragmentManager = getFragmentManager();
				fragmentManager.beginTransaction()
						.replace(R.id.frame_container, fragment).commit();

				// update selected item and title, then close the drawer
				mDrawerList.setItemChecked(0, true);
				mDrawerList.setSelection(0);
				setTitle(navMenuTitles[0]);
				mDrawerLayout.closeDrawer(mDrawerList);
			} else {
				Toast.makeText(getApplicationContext(),
						"Kiểm tra lại kết nối internet", Toast.LENGTH_LONG)
						.show();
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.vnexpress, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		// Handle action bar actions click
		switch (item.getItemId()) {
		case R.id.action_settings:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	/**
	 * When using the ActionBarDrawerToggle, you must call it during
	 * onPostCreate() and onConfigurationChanged()...
	 */

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggls
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	/**
	 * Slide menu item click listener
	 * */
	private class SlideMenuClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if (CheckInternet.checkInternetConnection(getApplicationContext())) {
				displayView(position);
			} else {
				Toast.makeText(getApplicationContext(),
						"Kiểm tra lại kết nối internet", Toast.LENGTH_LONG)
						.show();
			}
		}
	}

	private void displayView(int position) {
		// update the main content by replacing fragments
		Fragment fragment = null;
		Bundle bundle = new Bundle();
		switch (position) {
		case 0:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.TRANGCHU);
			fragment.setArguments(bundle);
			break;
		case 1:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.THOISU);
			fragment.setArguments(bundle);
			break;
		case 2:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.GIAODUC);
			fragment.setArguments(bundle);
			break;
		case 3:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.THEGIOI);
			fragment.setArguments(bundle);
			break;
		case 4:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.KINHDOANH);
			fragment.setArguments(bundle);
			break;
		case 5:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.GIAITRI);
			fragment.setArguments(bundle);
			break;
		case 6:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.THETHAO);
			fragment.setArguments(bundle);
			break;
		case 7:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.PHAPLUAT);
			fragment.setArguments(bundle);
			break;
		case 8:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.DULICH);
			fragment.setArguments(bundle);
			break;
		case 9:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.KHOAHOC);
			fragment.setArguments(bundle);
			break;
		case 10:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.SOHOA);
			fragment.setArguments(bundle);
			break;
		case 11:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.XE);
			fragment.setArguments(bundle);
			break;
		case 12:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.CONGDONG);
			fragment.setArguments(bundle);
			break;
		case 13:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.TAMSU);
			fragment.setArguments(bundle);
			break;
		case 14:
			fragment = new New_Fragment();
			bundle.putString("urlLink", RssLink.CUOI);
			fragment.setArguments(bundle);
			break;
		default:
			break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.frame_container, fragment).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			mDrawerList.setSelection(position);
			setTitle(navMenuTitles[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
		} else {
			// error in creating fragment
			Log.e("MainActivity", "Error in creating fragment");
		}
	}
}
